package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.vo.SkuInfoVo;
import com.atguigu.gmall.product.vo.SpuInfoVo;
import com.atguigu.gmall.starter.cache.service.CacheService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.atguigu.gmall.common.constant.RedisConst.CACHE_SKU_DETAIL_PREFIX;

@RestController
@RequestMapping("admin/product")
public class SkuManageController {

    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    CacheService cacheService;
    // http://weiranyi.xiaomy.net/admin/product/list/1/10
    @GetMapping("/list/{page}/{limit}")
    public Result skuList(@PathVariable("page") long pn,
                          @PathVariable("limit") long ps) {
        Page<SkuInfo> skuInfoPage = skuInfoService.getSkuList(pn, ps);
        return Result.ok(skuInfoPage);
    }

    // http://weiranyi.xiaomy.net/admin/product/spuSaleAttrList/31
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfoVo skuInfoVo) {
        skuInfoService.saveSkuInfo(skuInfoVo);
        return Result.ok();
    }

    // 修改
    @GetMapping("/updateSkuInfo")
    public Result updateSkuInfo(@RequestBody SkuInfoVo vo) {
        // 1.修改数据库

        // 2.删除缓存
        cacheService.delayDoubleDelete(RedisConst.CACHE_SKU_DETAIL_PREFIX+"51");
        return Result.ok();
    }

    // 上架：http://weiranyi.xiaomy.net/admin/product/onSale/48[1]
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {
        skuInfoService.onSale(skuId);
        return Result.ok();
    }

    // 下架：http://weiranyi.xiaomy.net/admin/product/cancelSale/43[0]
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {
        skuInfoService.cancelSale(skuId);
        return Result.ok();
    }
}
